<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Posts extends Model
{
    protected $table = 'posts';

    protected $fillable = [
        'object_id',
        'object_type',
        'title',
        'subtitle',
        'pictures',
        'content',
        'view_amount',
        'like_amount',
        'collect_amount',
        'sort',
        'is_show',
    ];

    protected $casts = [
        'is_show' => 'boolean',
    ];

    static $IS_SHOW_MAP = [
        '否', '是',
    ];

    public function object()
    {
        return $this->morphTo()->withDefault();
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'object');
    }

    public function setPicturesAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['pictures'] = implode(',', $pictures);
        } else {
            $this->attributes['pictures'] = $pictures;
        }
    }

    public function getPicturesAttribute($pictures)
    {
        if ($pictures) {
            return explode(',', $pictures);
        } else {
            return [];
        }
    }
}
